﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using GenericLibrariesToolKit;
using Model;
using BLL.Info;
using BLL.Info.Impl;
using BLL.Users.Impl;
using BLL.Users;
using BLL.Privilege;
using BLL.Privilege.Impl;
using Utils;

public partial class Page_Publications_PublicationsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            txtPublicationsPostAuthor.Text = WebUtil.GetIdentifier(Session["userId"]);
            showPublicationList();
            //发表出版物权限控制
            PostPublicationPrivilegeControl();
    }

    /// <summary>
    /// 发表出版物
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPublicationsListPostSubmit_Click1(object sender, ImageClickEventArgs e)
    {
        
        //获取数据和数据验证
        string publicationTitle = txtPublicationsPostTitle.Text.Trim().ToString();
        string publicationType = ddlPublicationsPostType.Text.Trim().ToString();
        string publicationAuthor = txtPublicationsPostAuthor.Text.Trim().ToString();
        string publicationDate = txtPublicationsPostPubDate.Text.Trim();
        float publicationPrice = 0;
        string publicationIntroduce = txtPublicationsPostIntroduce.Text.Trim().ToString();
        
        try
        {
            publicationPrice = float.Parse(txtPublicationsPostPrice.Text.Trim().ToString());
        }
        catch(Exception)
        {
            Response.Write("<script>alert('" + "Price textbox input error!" + "')</script>");
            return;
        }
        //验证是否存在该作者
        UserBO userBO = new UserBOImpl();
        User resultUser = userBO.Find(publicationAuthor);
        if (resultUser ==null)
        {
            Response.Write("<script>alert('" + " The author not exist!" + "')</script>"); 
            return;
        }
        //封装数据
        Publication publication = new Publication
        {
            PublicationTitle = publicationTitle,
            PublicationType = publicationType,
            PublicationAuthor = publicationAuthor,
            PublicationPrice = publicationPrice,
            PublicationDescription = publicationIntroduce,
            PublicationDate = publicationDate,
        };
        //添加出版物到数据库
        PublicationBO publicationBO = new PublicationBOImpl();
        bool result = publicationBO.Add(publication);
        //清空文本框页面
        clearData();
        if(result)
            showPublicationList();
    }

    /// <summary>
    /// 显示出版物列表
    /// </summary>
    public void showPublicationList()
    {
        PublicationBO publicationBO = new PublicationBOImpl();
        //设置order by 排序
        Dictionary<string, string> orderBy = new Dictionary<string, string>();
        orderBy.Add("PublicationDate", "desc");

        DataSet datasetNews = publicationBO.GetDataSet(-1,-1,null,orderBy);
        gvPublicationList.DataSource = datasetNews;
        gvPublicationList.DataBind();
    }

    /// <summary>
    ///对发表出版物进行权限控制
    /// </summary>
    public void PostPublicationPrivilegeControl()
    {
        //获取用户ID
        string userID = WebUtil.GetIdentifier(Session["userId"]);
        /*--test--
        RoleModelRight roleModeRight = new RoleModelRightImpl();
        roleModeRight.GetRoleRight(userID);
        --test--*/
        bool result = isPostPublicationPrivilege(userID);
        //隐藏发表出版物DIV,显示菜单栏DIV
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", GenerateScript(result));
    }

    /// <summary>
    /// 判断用户是否有发表出版物的权限
    /// </summary>
    /// <returns></returns>
    public bool isPostPublicationPrivilege(string userID)
    {
        RoleRightBO roleRightBO = new RoleRightBOImpl();
        //如果用户为游客，返回false
        if (userID == null)
            return false;

        bool result = roleRightBO.IsPrivilege(userID, "publicationsInfo", OperateType.add);
        return result;
    }
    /// <summary>
    /// 生成js脚本
    /// </summary>
    /// <param name="isShowLoginMaster"></param>
    /// <returns></returns>
    private string GenerateScript(bool isShowLoginMaster)
    {
        //存放script的容器
        StringBuilder script = new StringBuilder();
        script.Append("<script>");
        if (isShowLoginMaster)
        {
            script.Append("document.getElementById('PostPublication').style.display='block';");
        }
        else
        {
            script.Append("document.getElementById('PostPublication').style.display='none';");
        }
        script.Append("</script>");

        return script.ToString();
    }
    /// <summary>
    /// 分页处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvPublicationList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvPublicationList.PageIndex = e.NewPageIndex;
        this.gvPublicationList.DataBind();
    }
    //返回主页面
    protected void btnPublicationsBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");

    }
    //清空文本框页面
    private void clearData()
    {
        txtPublicationsPostTitle.Text="";
        ddlPublicationsPostType.Text="";
        txtPublicationsPostIntroduce.Text="";
        txtPublicationsPostPubDate.Text = "";
    }
}